package com.accounting.client.frontend.product.search;

import com.accounting.client.frontend.product.ProductManager;
import com.accounting.client.remote.Services;
import com.accounting.client.remote.obj.Product;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.Util;
import corelib.client.search.JSONSearchRequest;
import corelib.client.search.PagingRequest;
import corelib.client.search.PagingResult;
import corelib.client.ui.SimpleMask;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.button.Button;
import corelib.client.ui.widget.form.LazyInitializer;
import corelib.client.ui.widget.form.TextField;
import corelib.client.ui.widget.layout.RelativeColumn;
import corelib.client.ui.widget.panel.SimpleSearchPanel;

public class ProductSearchPanel extends SimpleSearchPanel {
	
	private final static int MaxRecrodsPerPage = 20;
	
	private ProductManager productManger;
	private TextField   keywordsField;
	
	public ProductSearchPanel(ProductManager page){
		this.productManger = page;
		this.setWidth(400);
	}
	
	@Override
	protected Widget getMaskWidget() {
		return productManger.getProductTable();
	}

	@Override
	protected Widget createContentPanel() {
		int lw  = 100;
		int fw  = 250;
		int gap = 12;
		
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(10, Unit.PX);
		RelativeColumn col = new RelativeColumn();
		
		col.addRow()
		   .add(LazyInitializer.createLabel("Keywords").setBold(true),lw + fw + gap);
		
		col.setRowSpacing(20);
		
		col.addRow()
		   .add(keywordsField = LazyInitializer.createTextBox("Name, Keyword etc"), lw + gap +fw);
		
		Button searchButton = new Button("Search", new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				search();
			}
		});
		Button resetButton = new Button("Reset", new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				reset();
			}
			
		});
		col.addRow().add(searchButton,100).addSpace(gap).add(resetButton,100);
		panel.add(col.getPanel());
		return panel;
	}
	
	private void reset(){
		keywordsField.setText("", false);
		search();
	}
	
	private void search() {
		String passport = SessionManager.getPassport();
		PagingRequest request = new PagingRequest();
		request.setOffset(0);
		request.setLimit(MaxRecrodsPerPage);
		request.setKeyword(keywordsField.getTrimmedText());
		
		SimpleMask.show(getMaskWidget(), "Searching...");
		Services.ProductService.SearchProducts(passport, request , new AsyncCallback<PagingResult<Product>>(){
			@Override
			public void onFailure(Throwable caught) {
				SimpleMask.hide(getMaskWidget());
				ErrorHandler.handleServiceError(caught);
			}
			
			@Override
			public void onSuccess(PagingResult<Product> result) {
				SimpleMask.hide(getMaskWidget());
				productManger.getProductTable().setObjects(result.getData());
			}
		});
	}
	
	@Override
	protected JSONSearchRequest createSearchRequest() {
		if (Util.isBlank(keywordsField.getTrimmedText())){
			keywordsField.markRequired();
			return null;
		}
		return new JSONSearchRequest();
	}

}
